package client;

import javax.annotation.Resource;
import javax.jws.HandlerChain;
import javax.jws.WebService;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMResult;
import javax.xml.ws.Provider;
import javax.xml.ws.Service.Mode;
import javax.xml.ws.ServiceMode;
import javax.xml.ws.WebServiceContext;
import javax.xml.ws.WebServiceProvider;

import org.w3c.dom.Node;


/**
 * Callback web service - dynamic.
 * 
 */
//@WebServiceProvider
@WebService
@ServiceMode(Mode.PAYLOAD)
@HandlerChain(file = "callbackchain.xml")
public class CallBackDinamic implements Provider<Source> {

	@Resource
	private WebServiceContext context;

	public Source invoke(Source source) {
		System.out.println("Asynch response received");
		// print results
		try {
			DOMResult dom = new DOMResult();
			Transformer trans = TransformerFactory.newInstance().newTransformer();
			trans.transform(source, dom);
			Node node = dom.getNode();
			System.out.println(node.getChildNodes().item(0).getTextContent());
		} catch (Exception ex) {
			throw new RuntimeException(ex);
		}

		System.out.println("This response relates to the message ID: " + context.getMessageContext().get("RelatesTo"));
		 
			
		// it's a one-way operation
		return source;
	}
}
